package neu.soft.facade;

import lombok.extern.slf4j.Slf4j;
import neu.soft.binder.ChannelBinder;
import neu.soft.entity.FinancialPurchaseOrder;
import neu.soft.exception.AppException;
import neu.soft.exception.AppExceptionCodeMsg;
import neu.soft.service.PurchaseService;
import neu.soft.vo.MsgBean;
import org.apache.rocketmq.common.message.MessageConst;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;
import org.springframework.util.MimeTypeUtils;


//消息发送者
@Component
@Slf4j
public class MessageSender {

    @Autowired
    PurchaseService purchaseService;

    @Autowired
    ChannelBinder channelBinder;

    // 发送购货单给业务中心
    public boolean sendPurchaseOrder(Integer financialOrderId) {
        FinancialPurchaseOrder financialPurchaseOrder = purchaseService.findById(financialOrderId);
        if (financialPurchaseOrder == null) {
            throw new AppException(AppExceptionCodeMsg.QUERY_FAIL);
        }
        MessageBuilder<MsgBean<FinancialPurchaseOrder>> builder = MessageBuilder.withPayload(new MsgBean<FinancialPurchaseOrder>(financialPurchaseOrder, "购货单已确认"))
                .setHeader(MessageConst.PROPERTY_DELAY_TIME_LEVEL, 0)
                .setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.APPLICATION_JSON);
        Message<MsgBean<FinancialPurchaseOrder>> message = builder.build();
        log.info("消息发送成功！等待消费");
        return channelBinder.supplyOutput().send(message);
    }

    //发送购货单给模拟供应商
    public boolean sendInfo(Integer operationId) {
        MessageBuilder<MsgBean<Integer>> builder = MessageBuilder.withPayload(new MsgBean<Integer>(operationId, "发票已经登记"))
                .setHeader(MessageConst.PROPERTY_DELAY_TIME_LEVEL, 0)
                .setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.APPLICATION_JSON);
        Message<MsgBean<Integer>> message = builder.build();
        return channelBinder.mockOutput().send(message);
    }


}
